package com.atguigu.serviceedu.service.impl;

import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.mapper.EduTeacherMapper;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-08-23
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public void pageTeacherCondition(Page<EduTeacher> p3, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> wrapper= new QueryWrapper<>();
        String name = teacherQuery.getName();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.gt("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.lt("gmt_create",end);
        }
        if(level!=null){
            wrapper.like("level",level);
        }
        wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(p3, wrapper);
    }

    @Override
    public Map<String, Object> pageTeacherFront(Page<EduTeacher> pageTeacher) {
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("id");
        baseMapper.selectPage(pageTeacher,wrapper);
        Map<String,Object> map=new HashMap<>();
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        long pages = pageTeacher.getPages();
        long current = pageTeacher.getCurrent();
        long size = pageTeacher.getSize();
        boolean hasNext= pageTeacher.hasNext();
        boolean hasPrevious = pageTeacher.hasPrevious();
        map.put("total",total);
        map.put("list",records);
        map.put("pages",pages);
        map.put("current",current);
        map.put("size",size);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map;
    }

}
